
//
//  ZGSMyCollectionViewController.m
//  Tour
//
//  Created by dllo on 15/11/2.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ZGSMyCollectionViewController.h"
#import "ZGSDestinationCity.h"
#import "ZGSDataBaseHandle.h"
//#import "ZGSQQQQQQQQQQ.h"
#import "ZGSDestinationCityController.h"

@interface ZGSMyCollectionViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *arr;

@end

@implementation ZGSMyCollectionViewController

- (void)dealloc{
    [_movie release];
    [_tableView release];
    [_arr release];
    [super dealloc];
}

- (void)viewWillAppear:(BOOL)animated{
    self.arr = [NSMutableArray array];
    self.arr = [[ZGSDataBaseHandle shareDataBaseHandle] selectAllMovie];
    if (self.arr.count == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"没有数据" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self.navigationController popViewControllerAnimated:YES];
            
        }];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
        
        
        
    }
    [self.tableView reloadData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
//    [_tableView release];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    NSLog(@"%@--------------------", self.arr);
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    
    [_tableView release];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    ZGSQQQQQQQQQQ *zgscity = [[ZGSQQQQQQQQQQ alloc] init];
    zgscity = self.arr[indexPath.row];
    
    cell.textLabel.text = zgscity.cnname;
//     [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
//    NSLog(@"%@--------------------", );
//    [self.tableView reloadData];

    return cell;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //         一定要删除对应数据源的数据
        ZGSQQQQQQQQQQ *news = [self.arr objectAtIndex:indexPath.row];
        [[ZGSDataBaseHandle shareDataBaseHandle] deleteMovieInDB:news];
        [self.arr removeObjectAtIndex:indexPath.row];
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [self.tableView reloadData];
        if (self.arr.count == 0) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"警告" message:@"没有数据" preferredStyle:UIAlertControllerStyleAlert];
            
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self.navigationController popViewControllerAnimated:YES];
                
            }];
            [alert addAction:cancel];
            [self presentViewController:alert animated:YES completion:^{
                
                
            }];
            
            
            
        }

        
        
    }
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZGSDestinationCityController *zgsdesVC = [[ZGSDestinationCityController alloc] init];
    ZGSQQQQQQQQQQ *zgsq = [[ZGSQQQQQQQQQQ alloc] init];
    zgsq = self.arr[indexPath.row];
    zgsdesVC.cityid = zgsq.countryid;
    zgsdesVC.movie = self.arr[indexPath.row];
    zgsdesVC.hidesBottomBarWhenPushed = YES;
    
    
    

    
   
    [self.navigationController pushViewController:zgsdesVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
